home *** CD-ROM | disk | FTP | other *** search
/ MaxiMac 2001 July / MaxiMac 116.iso / Macworld on CD n°116 / Mac OS X / Internet / Utilisation / NotifyMail Folder / Help / nmfinger.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-04-02  |  3.5 KB  |  135 lines  |  [TEXT/R*ch]

  1. /*
  2.  * Simplified version of the standard Internet "finger" program.
  3.  * It fingers the special purpose user "nm_notifyuser" on the computer
  4.  * given as the only argument.
  5.  *
  6.  * This program is meant to be called by the mail delivery program on
  7.  * a POP server. See below for how to invoke it.
  8.  * When fingered with the user name "nm_notifyuser", NotifyMail will
  9.  * tell the POP client to fetch mail from the POP server.
  10.  *
  11.  *
  12.  * It is (normally) invoked as follows:
  13.  *       nmfinger <machinename>
  14.  * where machinename is replaced by the IP address or DNS name of the
  15.  * machine running NotifyMail.
  16.  * 
  17.  * If NotifyMail is configured to use UDP, this program should be invoked
  18.  * as follows:
  19.  *       nmfinger <machinename> UDP
  20.  *
  21.  *
  22.  * It exits with a result code of 0 wether or not the computer
  23.  * replies. It doesn't listen for any answer.
  24.  *
  25.  * Use the options "-D_BSD -lbsd" to compile on AIX 4.1.
  26.  * PATH=/usr/bin:/usr/local/bin gcc -s -O nmfinger.c -o nmfinger -lsocket -lnsl
  27.  *
  28.  * Concept for this program and original prototype written by a NotifyMail user
  29.  * Modifications and redesign by SAG
  30.  */
  31.  
  32. #include <sys/types.h>
  33. #include <sys/socket.h>
  34. #include <sys/errno.h>
  35. #include <netinet/in.h>
  36. #include <unistd.h>
  37. #include <netdb.h>
  38. #include <stdio.h>
  39.  
  40. #define DATA "nm_notifyuser\r\n"
  41. #define BUFSIZE 256
  42.  
  43. main(argc, argv)
  44. int argc;
  45. char *argv[];
  46. {
  47.     int sock, flag;
  48.     struct sockaddr_in server;
  49.     struct hostent *hp;
  50.     char buffer[BUFSIZE];
  51.  
  52.     /* Read and discard data from standard input (the mail). */
  53.   
  54.     /* Under most circumstances, you should leave the following */
  55.     /* line uncommented as the mail will be piped into the stdin of this */
  56.     /* program. However, if you encounter problems, comment out the */
  57.     /* following line. */
  58.   
  59.     while (getc(stdin)!=EOF);
  60.  
  61.  
  62.     /* Create a socket. */
  63.     /* Normally create a TCP socket...if it is invoked for UDP, create a */
  64.     /* UDP socket. */
  65.  
  66.     sock = socket(AF_INET, SOCK_STREAM, 0);
  67.  
  68.     if (sock < 0)
  69.     {
  70.         exit(0);
  71.     }
  72.  
  73.     /* Look up the domain name given as argument. */
  74.     hp = gethostbyname(argv[1]);
  75.     if (hp == 0)
  76.     {
  77.         exit(0);
  78.     }
  79.  
  80.         /*
  81.          * In order to reduce the time we want to wait for the
  82.          * connection to NotifyMail to timeout, we'll do an fping
  83.          * to the host. If the ping returns very quickly, then
  84.          * try the connection. Make sure that fping is
  85.          * properly installed
  86.          */
  87.         strcpy(buffer,"which fping");
  88.         
  89.         if (system(buffer) ==0)
  90.         {
  91.   sprintf(buffer,"fping -q -t 250 %s",argv[1]);
  92.   
  93.   if (system(buffer) != 0)
  94.   {
  95.           exit(0);
  96.   }
  97.         }
  98.  
  99.     /* Build address structure. */
  100.     memcpy(&server.sin_addr,hp->h_addr,hp->h_length);
  101.  
  102.     server.sin_family = AF_INET;
  103.   
  104.     /* The following line may need to be uncommented depending on the OS version -SAG */
  105.     /* server.sin_len = sizeof(server); */
  106.     server.sin_port = htons(atoi("2079"));
  107.  
  108.     /* Connect to machine running NotifyMail. */
  109.     if (connect(sock, (struct sockaddr*)&server, sizeof(server)) < 0)
  110.     {
  111.         exit(0);
  112.     }
  113.  
  114.     /* Send finger request. */
  115.     if (write(sock, DATA, sizeof(DATA)-1) < 0)
  116.     {
  117.         exit(0);
  118.     }
  119.  
  120.     /* Recive answer and throw it away. */
  121.     /* Under most circumstances, you can leave the following block commented */
  122.     /* out; there is no reason to read the response (if any) from NotifyMail. */
  123.     /*
  124.     if (read(sock, buffer, BUFSIZE) < 0)
  125.     {
  126.         exit(0);
  127.     }
  128.     */
  129.  
  130.     /* Close socket. */
  131.     close(sock);
  132.  
  133.     exit(0);
  134. }
  135.